//获取验证码
var wait = 60;
var phoneReg = /^[1][0-9]{10}$/;
var codeReg = /^[0-9]{4}$/;
var pwdReg= /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;

//-----------------找回
var $forgetPhone = $("#forgetPhone");
var $forgetCode = $("#forgetCode");
var $forgetPassword1 = $("#forgetPassword1");
var $forgetPassword2 = $("#forgetPassword2");
var $eyeOpen = $(".js-eye-open");

var flagforgetPhone = false;//公共flag 判断是不是验证通过
var flagforgetCode = false;//公共flag 判断是不是验证通过
var flagforgetPassword1 = false;//公共flag 判断是不是验证通过
var flagforgetPassword2 = false;//公共flag 判断是不是验证通过

(function(){
		

        //查看明文密码
        $eyeOpen.on("click", lookPwd);
        $("#forgetBtn").on("click", forgetSubmit);
        
        //手机号输入框事件
        $forgetPhone.on("onreadystatechange input blur", function(){
          var $this = $(this);
          var forgetPhone = $this.val();

          flagforgetPhone = validateForm($this, forgetPhone, phoneReg, "请输入正确的手机号");
        });

        //验证码输入框事件
        $forgetCode.on("onreadystatechange input blur", function(){
          var $this = $(this);
          var $cleartext = $this.next("input[type='text']");
          var forgetCode = $this.val();

          flagforgetCode = validateForm($this, forgetCode, codeReg, "请输入正确的验证码", $cleartext);
        });

        //密码输入框事件
        $forgetPassword1.on("onreadystatechange input blur", function(){
          var $this = $(this);
          var $cleartext = $this.next("input[type='text']");
          var forgetPassword = $this.val();

          $cleartext.val(forgetPassword);

          flagforgetPassword1 = validateForm($this, forgetPassword, pwdReg, "请输入正确的密码", $cleartext);
        });

        //明文密码输入框事件
        $forgetPassword2.on("onreadystatechange input blur", function(){
            var $this = $(this);
            var $cleartext = $this.prev("input[type='password']");
            var forgetPassword = $this.val();

            $cleartext.val(forgetPassword);

            flagforgetPassword2 = validateForm($this, forgetPassword, pwdReg, "请输入正确的密码", $cleartext);
        });

        //表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
        function validateForm(that, value, reg, txt, cleartext){
              var $parent = that.closest(".form-group");
              var $addon = $parent.find(".input-group-addon");
              var $msg = $parent.find(".js-line-alert");
              var $msgText = $parent.find(".js-alert-text");
              
              var flag = true;

              if(value.length === 0){
                modifyWarning($msgText, txt);
                showWarning($addon, that, $msg, cleartext);
                flag = false;
              }
              else if(!reg.test(value)){
                modifyWarning($msgText, txt);
                showWarning($addon, that, $msg, cleartext);
                flag = false;
              }
              else{
                hideWarning($addon, that, $msg, cleartext);
              }

              return flag;
        }

        //输入框边框变红并显示提示文字
        function showWarning(addon, input, msg, cleartext){
            if(cleartext){
              cleartext.addClass("input-error border-error");
            }
            addon.addClass("border-error");
            input.addClass("input-error border-error");
            msg.show();
        }

        //输入框边框恢复原色并隐藏提示文字
        function hideWarning(addon, input, msg, cleartext){
            if(cleartext){
              cleartext.removeClass("input-error border-error");
            }
            addon.removeClass("border-error");
            input.removeClass("input-error border-error");
            msg.hide();
        }

        //修改提示文字信息
        function modifyWarning(element, txt){
            element.text(txt);
        }

        //查看明文密码
          function lookPwd(){
            var $pwd = $(this).closest(".input-group").find("input[type='password']");
            var $pwd_show = $pwd.next("input[type='text']");
            var pwd = $pwd.val();
            var pwd_show = $pwd_show.val();

            if($pwd.is(":visible")){
                $pwd.hide();
                $pwd_show.val(pwd).show();
            }
            else{
                $pwd.val(pwd_show).show();
                $pwd_show.hide();
            }
          }

		//注册
    function forgetSubmit(){
			var phone = $('#forgetPhone').val();
      var pwd = $('#forgetPassword1').val();
			var token = $('#forgetCode').val();

      $forgetPhone.blur();
      $forgetCode.blur();
      $forgetPassword1.blur();
      $forgetPassword2.blur();  
			
			if(flagforgetPhone && flagforgetCode && flagforgetPassword1 && flagforgetPassword2){
				$.ajax({
        			url: "forgetPwd",
        			type: "post",
        			dataType: 'json',
        			data : {
        				phone : phone,
        				pwd : pwd,
						    token :token
        			},
        			success: function(data) {
        				if (data.returnFlag == '0') {
                            layer.open({
                                content: '您的密码修改完成，请重新登录!',
                                scrollbar: false
                            });
        					location.href = "../user/loginPage";
        				}else {
									if (data.returnCode == 'A002') {
                                        layer.open({
                                            content: '您的帐号不存在!',
                                            scrollbar: false
                                        });
		        					}else{
                                        layer.open({
                                            content: '您的验证码错误!',
                                            scrollbar: false
                                        });
									}
        				}
        			},
        			error: function(xhr, type, errorThrown) {
        				//异常处理；
                        alert("请稍后再试!");
        			}
        		});
			}
    }
		
  var captchaBtn = document.getElementById("captchaBtn");

  captchaBtn.addEventListener("click", function() {
    var $this = $(this);
    var phone = $("#forgetPhone").val();

    var flag = validateForm($forgetPhone, phone, phoneReg, "请输入正确的手机号");

    if (flag) {
      sendCode(captchaBtn);
      timeChange(captchaBtn);
    } else {
      return false;
    }
  });
    
  //验证码时间改变
  function timeChange(btn) {
    if (wait === 0) {
      $(btn).removeClass("c-red");
      btn.removeAttribute("disabled");
      btn.innerText = "发送验证码";
      wait = 60;
    } else {
      $(btn).addClass("c-red");
      btn.setAttribute("disabled", true);
      btn.innerText = "(" + wait + ")秒后重新获取";
      wait--;
      setTimeout(function(){
        timeChange(btn);
      }, 1000);
    }
  }
    
  //发送验证码
  function sendCode(btn) {
    var phone = $("#forgetPhone").val();

    if (btn.innerText == "发送验证码") {
      $.ajax({
        url: "../apptoken/sendToken",
        type: "get",
        data: {
          phone : phone
        },
        dataType: 'json',
        success: function(data) {
          if (data.returnFlag != 0) {
            flagforgetPhone = validateForm($this, phone, phoneReg, "请输入正确的手机号");
          }
        },
        error: function(xhr, type, errorThrown) {
          //异常处理；
          flagforgetPhone = validateForm($this, phone, phoneReg, "请稍后在试！");
        }
      });
    } else {
      return false;
    }
  }
        
})();